<template>
  <div>
    <div class="topSearch">
      <span>角色名：<el-input clearable size="small" style="width: 300px;margin-bottom: 10px" placeholder="请输入角色名"
                          v-model="roleName"></el-input></span>
      <span><el-button size="mini" type="primary" style="margin-left: 15px" @click="getList"> 搜索</el-button></span>
    </div>
    <div class="centerTable">
      <el-table :data="roleData" border height="calc(100vh - 180px)"
                :headerCellStyle="{
              'textAlign': 'center',
              'background': '#66b1ff',
              'color': 'white',
            }" :cellStyle="{ textAlign: 'center', 'font-size': '12px'}">
        <el-table-column label="序号" type="index" width="80px"></el-table-column>
        <el-table-column label="角色ID" prop="id" width="80px"></el-table-column>
        <el-table-column label="角色名" prop="name"></el-table-column>
        <el-table-column label="状态" prop="status">
          <template v-slot:default="scope">
            <span>{{ scope.row.status == 1 ? '正常' : '已禁用' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" prop="op">
          <template v-slot:default="scope">
            <el-button size="small" type="danger" v-if="scope.row.status==1&&authority.includes(1311)"
                       @click="getStatus(scope.row,0,'禁用')">禁用
            </el-button>
            <el-button size="small" type="success" v-if="scope.row.status!=1&&authority.includes(1311)"
                       @click="getStatus(scope.row,1,'启用')">启用
            </el-button>
            <el-button size="small" type="warning" @click="splitAdd(scope.row)" v-if="authority.includes(1312)">配置权限
            </el-button>
            <el-button size="small" type="info" @click="splitArea(scope.row)" v-if="authority.includes(1313)">仓库角色权限
            </el-button>
          </template>
        </el-table-column>

      </el-table>

    </div>
    <assign-permissions ref="splitAssign"></assign-permissions>
    <el-dialog :visible.sync="areaPermissionsDialog" title="分配仓库权限" center width="35%">
      <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">全选</el-checkbox>
      <div style="margin: 15px 0;"></div>
      <div style="display: flex;align-items: center;justify-content: center">
        <el-checkbox-group v-model="checkedArea" @change="handleCheckedAreaChange"
                           style="width: 80%;display: flex;flex-wrap: wrap;align-items: center;justify-content: flex-start">
          <el-checkbox v-for="item in areaData" :label="item.area" :key="item.area" style="width: 100px">
            {{ item.areaName }}
          </el-checkbox>
        </el-checkbox-group>
      </div>
      <span slot="footer">
          <el-button size="small" type="info" @click="areaPermissionsDialog=false">取 消</el-button>
        <el-button size="small" type="primary" @click="submitArea">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import assignPermissions from "./components/assignPermissions.vue";
import quinn from "../../utils/quinn";

export default {
  name: "roleList",
  components: {
    assignPermissions
  },
  data() {
    return {
      isIndeterminate: true,
      roleData: [],
      roleName: "",
      checkAll: false,
      areaData: [],
      checkedArea: [],
      authority: [],
      areaPermissionsDialog: false,
      params: {
        pageNumber: 1,
        pageSize: 20
      }
    }
  },
  created() {
    const {authorityList = []} = quinn.getStorage('userData')
    this.authority = authorityList
    this.getList()
  },
  methods: {
    getList() {
      this.$api.roleGetRoleList({roleName: this.roleName}).then(res => {
        this.roleData = res
      })
    },
    splitAdd(val) {
      this.$refs.splitAssign.handleOpen(val)
    },
    getStatus(row, type, typeName) {
      const data = {
        roleId: row.id,
        status: type
      }
      this.$confirm(`${typeName + row.name} 角色？`, '提示', {
        confirmButtonText: '确认',
        type: 'warning',
        confirmButtonClass: 'el-button--success',
        cancelButtonClass: 'el-button--info',
      }).then(() => {
        this.$api.roleUpdateRoleStatus(data).then(res => {
          if (res == 1) {
            this.$message.success('修改成功！')
            this.getList()
          }
        })
      })
          .catch((action) => {
          });
    },
    splitArea(row) {
      this.areaPid = row.id
      this.$api.areaList().then(res => {
        this.areaData = res
        this.$api.areaGetWarehouseId({roleId: row.id}).then(res2 => {
          this.checkedArea = res2
          if (this.areaData.length == res2.length) {
            this.checkAll = true
          } else {
            this.checkAll = false
          }
        })
        this.areaPermissionsDialog = true
      })
    },
    handleCheckedAreaChange() {

    },
    _search() {

    },
    handleCheckAllChange(val) {
      this.checkedArea = val ? this.areaData.map(item => item.area) : [];
      this.isIndeterminate = false;
    },
    submitArea() {
      const data = {
        roleId: this.areaPid,
        list: this.checkedArea
      }
      this.$api.areaPermission(data).then(res => {
        if (res == 1) {
          this.$message.success('操作成功！')
          this.areaPermissionsDialog = false
          this.getList()
        }
      })
    }
  }
}
</script>

<style scoped lang="less">
/deep/ .el-dialog__header {
  background-color: #0f79e2;
  //border-top-left-radius: 20px;
  //border-top-right-radius: 20px;
}

/deep/ .el-dialog__title {
  color: #fff;
}

</style>